﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.AcmsControls;

public partial class MasterPage : System.Web.UI.MasterPage
{
    private bool requiresDataBinding = true;
    public override void DataBind()
    {
        requiresDataBinding = false;
        base.DataBind();
    }

    
    //protected override void OnPreRender(EventArgs e)
    protected override void  OnPreRender(EventArgs e)
    {
        if(!Page.IsPostBack && requiresDataBinding)
        {
            DataBind();
        }
        base.OnPreRender(e);

        //base.PreRender;
    }
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
     
    }

    protected void dsBreadcrumbs_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        var acmsPage = this.Page as Acms.AcmsControls.TemplatePage;
        if (acmsPage != null && acmsPage.AcmsContext.CurrentPost != null)
        {
            var posts = new List<Post>();
            posts.Add(acmsPage.AcmsContext.CurrentPost);
            while (posts.Last().Parent != null)
            {
                posts.Add(posts.Last().Parent);
            }
            posts.Reverse();
            e.SetData(posts);
        }
        else if(acmsPage != null)
        {
            //e.SetData(Acms.Bll.DummyPost.CreateDummyList(5));
            e.SetData(Acms.Bll.Post.GetTopLevelBySiteId(acmsPage.AcmsContext.SiteId, acmsPage.AcmsContext.SiteVersion));
        }
    }
    protected Acms.AcmsControls.TemplatePage AcmsPage
    {
        get
        {
            var tmp = this.Page as Acms.AcmsControls.TemplatePage;
            return tmp;
        }
    }
}
